/**
 * 渠道价格维护 - 编辑页面
 * @author shixc
 * @date 2019/02/01
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            gwdb003: false,
            detail: false,
            form: '',
            rules: {
                GWDB001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDB003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDB005: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions: {
                gwdc007Options: app.datas_('crm_gwcd_gwcd007')
            }
        },
        cols = [[
            {field: 'GWDB002', type: 'checkbox', title: '选择'}
            , {field: 'GWDB012', width: 80, title: '客户编号', event: 'openGwak'}
            , {field: 'GWDB013', width: 80, title: '客户名称'}
            , {field: 'GWDB014', width: 120, title: '品号', event: 'openGwam'}
            , {field: 'GWAML003', width: 120, title: '品名'}
            , {field: 'GWAML004', width: 120, title: '规格'}
            , {field: 'GWDB015', width: 80, title: '单位说明'}
            , {field: 'GWDB016', width: 80, title: '结算价', edit: 'text'}
        ]],
        callObj,
        moduleName,
        listData = null,
        pageVue,
        table, gwdc, gwdbTable, layOpenIndex
    ;

    var Gwdc002Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwdc002_edit',
                    _loadTplsFull: [
                        { dom: 'common_open_window', tpl: 'common/_open_window' }
                    ]
                }, win);
            },
            initTable: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwdbTable = table.render({
                        elem: app.getByid(doc, 'gwdc002_edit_table'),
                        id: 'gwdc002_edit_table',
                        // size: _layuiConfig.ui_size,
                        // autoSort: false, //禁用前端自动排序
                        cols: cols,
                        data: listData,
                        //loading: true,
                        page: false,
                        height : 300,
                        where: '',
                        // done: function (res, curr, count) {
                        // }
                    });

                    table.on('tool(gwdc002_edit_table)', function (obj) {
                        if (obj.event === 'openGwak') {
                            if (vueData.form.GWDB003 == '2') { // 渠道商
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            obj.updateOfEdit({
                                                'GWDB012': data.GWAK001,
                                                'GWDB013': data.GWAKL003
                                            });
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else {  // 渠道
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwae001',
                                            searchParam: {
                                                hasPermission: true
                                            },
                                            columnList: [
                                                {label: '销售渠道编号', prop: 'GWAE001', width: '150'},
                                                {label: '说明', prop: 'GWAEL003', width: '150'},
                                                {label: '助记码', prop: 'GWAEL004', width: '150'},
                                            ],
                                        },
                                        cb: function (data) {
                                            obj.updateOfEdit({
                                                'GWDB012': data.GWAE001,
                                                'GWDB013': data.GWAEL003
                                            });
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择销售渠道',
                                        area: ['600px', '465px']
                                    }
                                });

                            }
                        } else if (obj.event == 'openGwam') {
                            var keyArray=[];
                            var list = gwdbTable.getDatas();
                            for(var i =0;i<list.length;i++){
                                keyArray.push(
                                    list[i]['GWDB014']
                                );
                            }
                            var keyArrayStr=keyArray.join(',');
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    multiple: true,
                                    config: {
                                        method: 'q_gwam001',
                                        searchParam: {
                                            keyArrayStr:keyArrayStr
                                        },
                                        columnList: [
                                            { label: '产品编号', prop: 'GWAM001', width: '150'},
                                            { label: '品名', prop: 'GWAML003', width: '150'},
                                            { label: '规格', prop: 'GWAML004', width: '150'},
                                            { label: '单位', prop: 'GWABL003', width: '150',unshow:true}
                                        ],
                                        dataList: [],
                                        key: 'GWAM001'
                                    },
                                    cb: function (data) {
                                        var list = data;
                                        var listData = gwdbTable.getDatas();
                                        var select = [];
                                        if (list.length > 0) {
                                            for (var i = 0; i < list.length; i++) {
                                                select.push({
                                                    'GWDB002': listData.length + i,
                                                    'GWDB012': obj.data.GWDB012,
                                                    'GWDB013': obj.data.GWDB013,
                                                    'GWDB014': list[i].GWAM001,
                                                    'GWAML003': list[i].GWAML003,
                                                    'GWAML004': list[i].GWAML004,
                                                    'GWDB015': list[i].GWABL003,
                                                    'GWDB016': ''
                                                })
                                            }
                                            if (select.length > 0) {
                                                listData.removeByMap({
                                                    GWDB002: obj.data.GWDB002  // 清除新加的空行
                                                });
                                                listData.push.apply(listData, select);
                                            }
                                            self.tableReload(listData);
                                        }
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择产品',
                                    area:['600px', '520px']
                                }
                            });
                        }
                    });
                    //监听单元格编辑
                    table.on('edit(gwdc002_edit_table)', function (obj) {
                        if (obj.field == 'GWDB016') {
                            if (!(/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    'GWDB016': ''
                                });
                                app.showError('活动价必需是大于0的数字');
                                return;
                            } else {
                                obj.updateOfEdit({
                                    'GWDB016': parseFloat(obj.value).toFixed(2)
                                });
                            }
                        }
                    });
                });
            },

            tableReload: function (data) {
                table.reload('gwdc002_edit_table', {
                    data: data
                });
            },

            addData: function (listData) {
                app.ajax({
                    xurl: 'crm/gwdc002/addData',
                    xdata: listData,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '渠道价格增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwdc002').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },
            editData: function (listData) {
                app.ajax({
                    xurl: 'crm/gwdc002/editData',
                    xdata: listData,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '渠道价格修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwdc002').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            _call: function (tagData) {
                pageVue.$refs['form'].resetFields();
                moduleName = tagData.moduleName;
                layOpenIndex = tagData.layOpenIndex;
                if (tagData.is_add) {
                    vueData.detail = false;
                    vueData.isAdd = true;
                    vueData.gwdb003 = false;
                    app.ajax({
                        xurl: 'crm/gwdc002/getGwdb001',
                        cb: function (result) {
                            if (result.ok) {
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: {
                                        GWDB001: result.data.data.GWDB001,
                                        GWDB004: result.data.data.GWDB004,
                                        GWDB005: result.data.data.GWDB004,
                                        GWDB007: result.data.data.GWDB007,
                                        GWDB007_NAME: result.data.data.GWDB007_NAME,
                                        GWDB008: result.data.data.GWDB008,
                                        GWDB008_NAME: result.data.data.GWDB008_NAME,
                                        GWDB003: '1'
                                    },
                                    reset: true
                                });
                                listData = [];
                                self.tableReload(listData);
                            }
                        }
                    });
                } else {
                    vueData.isAdd = false;
                    if (tagData.detail == true) {
                        vueData.detail = true;
                    } else {
                        vueData.detail = false;
                    }
                    callObj = tagData.obj.data;
                    app.ajax({
                        xurl: 'crm/gwdc002/listGwdb',
                        xdata: {GWDB001: callObj.GWDB001},
                        loading: true,
                        cb: function (result) {
                            if (result.ok) {
                                listData = result.data.data;
                                if (listData.length > 0) {
                                    vueData.gwdb003 = true;
                                }
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: callObj,
                                    reset: true
                                });
                                self.tableReload(listData);
                            }
                        }
                    });
                }

            },
            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['GWDB'], {
                    GWAML003: null, GWAML004: null, GWDB007_NAME: null, GWDB008_NAME: null,
                    GWDB009_NAME: null
                });
                pageVue = new Vue({
                    el: '#gwdc002_edit',
                    data: vueData,
                    watch: {},
                    methods: {
                        addData: function () {
                            var data = gwdbTable.getDatas();
                            var count = data.length;
                            data.push({
                                GWDB002: count + 1,
                                GWDB012: '',
                                GWDB013: '',
                                GWDB014: '',
                                GWAML003: '',
                                GWAML004: '',
                                GWDB015: '',
                                GWDB026: ''
                            });
                            self.tableReload(data);
                            if (data.length > 0) {
                                vueData.gwdb003 = true;
                            }
                        },
                        cancle: function () {
                            layer.close(layOpenIndex);
                        },
                        del: function () {
                            var list = gwdbTable.getDatas();
                            var checkStatus = table.checkStatus('gwdc002_edit_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        GWDB002: v.GWDB002
                                    });
                                });
                                if (list.length == 0) {
                                    vueData.gwdb003 = false;
                                }
                                self.tableReload(list);
                            }
                        },
                        saveData: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    var list = gwdbTable.getDatas();
                                    if (list.length == 0) {
                                        app.showError('商品信息不可为空!');
                                        return;
                                    }
                                    // if (vueData.form.GWDB006 < vueData.form.GWDB005) {
                                    //     app.showError('截止日期不可小于生效日期!');
                                    //     return;
                                    // }
                                    for (var i = 0; i < list.length; i++) {
                                        var v = list[i];
                                        if (v.GWDB012 == '' || v.GWDB012 == null
                                            || v.GWDB014 == '' || v.GWDB014 == null) {
                                            app.showWarning('请选择客户及商品!');
                                            return;
                                        }
                                        if (v.GWDB016 == '' || v.GWDB016 == null) {
                                            app.showWarning('结算价必填!');
                                            return;
                                        }
                                    }
                                    list.addMap(vueData.form, false);
                                    if (vueData.isAdd) {
                                        self.addData(list);
                                    } else {
                                        self.editData(list);
                                    }
                                }
                            });
                        },

                        openUser: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysuser_id',
                                        columnList: [{label: '申请人员编号', prop: 'ID', width: '150'},
                                            {label: '申请人员姓名', prop: 'NAME', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.GWDB007 = data.ID;
                                        vueData.form.GWDB007_NAME = data.NAME;
                                        vueData.form.GWDB008 = data.ORG_ID;
                                        vueData.form.GWDB008_NAME = data.NAME_ORG;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择申请人员',
                                    area: ['600px', '470px']
                                }
                            });
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwdc002_edit');
                    }
                });
                self.initTable();
            },
            _lazy: function () {
                this._super();
            }
        })
    ;

    return new Gwdc002Edit();

})
;
